﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace GameSnake
{
    public class GameRender
    {
        public Snake.SnakeItem item {get;set;}

        private bool isFieldRendered = false;

       public void RenderSnake(Snake snake)
       {
          
          /* 
           for (int i = 0; i < snake.SnakeBody.Count; i++)
           {               
               Console.SetCursorPosition(snake.SnakeBody[i]._X, snake.SnakeBody[i]._Y);
               Console.ForegroundColor = ConsoleColor.Red;
               Console.Write("*");
               Console.SetCursorPosition(0, 0);
               Console.ResetColor();
           }
          // Console.SetCursorPosition(snake.SnakeBody[snake.SnakeBody.Count - 1]._X, snake.SnakeBody[snake.SnakeBody.Count - 1]._Y);
         //  Console.Write(" ");
          
          */
           Console.CursorVisible = false;

           for (int i = snake.SnakeBody.Count - 1; i > 0; i--)
           {

               snake.SnakeBody[i]._X = snake.SnakeBody[i - 1]._X;
               snake.SnakeBody[i]._Y = snake.SnakeBody[i - 1]._Y;

           }

           for (int i = 0; i < snake.SnakeBody.Count; i++)
           {
               Console.ForegroundColor = ConsoleColor.Yellow;


               this.item = snake.SnakeBody[i];
               Console.SetCursorPosition(item._X, item._Y);
               Console.Write("*");

               Console.ResetColor();
           }
               Console.SetCursorPosition(snake.SnakeBody[snake.SnakeBody.Count - 1]._X, snake.SnakeBody[snake.SnakeBody.Count - 1]._Y);
               Console.Write(" ");
          
       }

        public void MySnakeRender(Snake snakeR)
       {       

            for(int i = 0; i < snakeR.SnakeBody.Count; i++)
            {
                Console.SetCursorPosition(snakeR.SnakeBody[i]._X, snakeR.SnakeBody[i]._Y);
                Console.Write("+");
                Console.SetCursorPosition(0, 0);                
            }
            
            




       }
         
       public void RenderFood(Food food) 
       {
           Console.SetCursorPosition(food._X,food._Y);
           Console.ForegroundColor = ConsoleColor.Cyan;
           Console.Write("#");
           Console.SetCursorPosition(0, 0);
           Console.ResetColor();
       }  
       public  void RenderField(Field field)
           {
               if (!isFieldRendered)
               {
                   Console.ForegroundColor = ConsoleColor.Blue;
                   for (int i = 0; i < field._Left; i++, Console.SetCursorPosition(i, 0))
                       Console.Write("0");
                   Console.SetCursorPosition(0, 0);

                   for (int i = 0; i < field._Top; i++, Console.SetCursorPosition(0, i))
                       Console.Write("0");
                   Console.SetCursorPosition(0, 0);

                   for (int i = 0; i < field._Left; i++, Console.SetCursorPosition(i, field._Top - 1))
                       Console.Write("0");
                   Console.SetCursorPosition(0, 0);

                   for (int i = 0; i < field._Top; i++, Console.SetCursorPosition(field._Left - 1, i))
                       Console.Write("0");
                   Console.SetCursorPosition(0, 0);
                   Console.ResetColor();

                   isFieldRendered = true;
               }
           }       
    }
}
